<template>
    <div class=" flex-row w-full">
        <el-divider content-position="left">基本信息</el-divider>
        <div class=" bg-blue-300 px-5 py-5 rounded-l-20">
            <el-row>
                <el-button v-if="!forceStop" round type="warning" @click="forceStop = !forceStop">暂停</el-button>
                <el-button v-if="forceStop" round type="success" @click="forceStop = !forceStop">继续</el-button>
                <el-button round type="danger" @click="debugInfo = !debugInfo">调试</el-button>
                <!-- <el-button round type="success" @click="clearLocalStorage('')">清理</el-button> -->
                <!-- <el-button round type="success" @click="getToken">取token</el-button> -->
            </el-row>
            <el-row class="px-5 py-5">
                <el-input v-model="configInfo.key" style="max-width: 600px" placeholder="请输入密钥">
                    <template #prepend>密钥:</template>
                </el-input>
            </el-row>
            <el-row class="px-5 py-5  items-center">
                <p class="color-black text-center font-bold">等待倍率:  </p>
                <el-input-number  v-model="configInfo.examRate" :min="1" :max="10">

                </el-input-number>
                <el-button round type="danger" @click="setExamRate">设置</el-button>
            </el-row>

        </div>
        <el-divider content-position="left">前置课程</el-divider>

        <!--  这里呢666？-->


        <el-divider v-show="debugInfo" content-position="left">调试区</el-divider>
        <div v-show="debugInfo" class=" bg-red-300 px-5 py-5 rounded-l-20">
            <el-row class="flex ">
                <el-button round @click="printLog('一条日志')">一条日志</el-button>
                <el-button round @click="testSelect('初级')">初级测试</el-button>
                <el-button round @click="testSelect('中级')">中级测试</el-button>
                <!-- <el-button round @click="middleChoiceExamStart">中级测试</el-button> -->
                <el-button round @click="allASelect">全选A</el-button>
                <el-button round @click="getChoiceQueryBody('等级')">获取题目信息</el-button>
                <!-- <el-button round @click="init">初始化</el-button> -->
                <!-- <el-button round @click="mainLearn">课程学习</el-button> -->
            </el-row>


        </div>
        <el-divider content-position="left">考试区</el-divider>
        <div class=" bg-blue-500 px-5 py-5 rounded-l-20">
            <el-row>
                <el-button round type="success" @click="baseExamStart">初级考试</el-button>
                <el-button round type="success" @click="middleChoiceExamStart">中级选择</el-button>

            </el-row>

        </div>


    </div>
</template>

<script setup lang="ts">
// @ts-nocheck
import { ref } from 'vue';


import { getOneBySelect, getOneBySelectList } from "../utils/jqUtil";
import {GmGet, GmPost, needStop, sleep} from "../utils/common";
import { errMsg,  okMsg, printLog } from "../utils/noticeUtil";
const testFlag = ref(false)
const allA = ref(false)
const debugInfo = ref(false)
const forceStop = ref(false)
const configInfo = ref({
    key: 'xxdlovo',
    url: 'https://exam.ezrpa.store',
    examRate: 3
})

// 设置等待倍率
const setExamRate = () => {
    if (configInfo.value.examRate && configInfo.value.examRate > 0) {
        window['examRate'] = configInfo.value.examRate
    } else {
        window['examRate'] = 3
    }
    okMsg('设置成功')
}

/**
 * 初级考试
 */
const baseExamStart = async () => {
    await choiceStart('初级')
}

// 自动做中级的选择题
const middleChoiceExamStart = async () => {
    await choiceStart('中级')

}

const testSelect = async (level: string) => {
    printLog(level + '测试')
    testFlag.value = true
    await choiceStart(level)
    testFlag.value = false
}
/**
 * 自动做选择题
 */
const choiceStart = async (examLevel) => {
    await needStop(forceStop.value)
    let submitBtn = getOneBySelect('.footer button:nth-child(3)', '提交按钮', false)
    if (!submitBtn) {
        errMsg('请先进入'+examLevel+'考试页面!')
        return
    }
    let click = true
    let size = 0
    let max = testFlag.value ? 0 : 50
    while (click) {
        await sleep(150)
        await needStop(forceStop.value)
        size = ++size
        // 获取下一题按钮
        let nextExamBtn = getOneBySelect('.footer button:nth-child(2)', '下一题按钮', false)
        click = !nextExamBtn?.getAttribute('disabled')
        // 做初级的选择题
        await choiceOptionQueryHandle(examLevel)
        if (size > max) {
            // 防止死循环
            return
        }
        nextExamBtn?.click()
        await sleep(150)
    }
    okMsg('选择题结束!')
    printLog('选择题结束!')
}

/**
 * 查询选项并勾选
 * @param level 考试等级
 */
const clickOption = async (level: string) => {
    let query = 'v=xxdlovo&'
    if (level === '初级') {

    }
}
const allASelect = async () => {
    allA.value = true
    await choiceStart('中级')
    allA.value = false
}

// 获取选择题查询字符串
const getChoiceQueryBody = async (examLevel: string) => {
    let itemType = getOneBySelect('.question h3', '题目类型', false)
    let examTypeStr = 'radio'
    if (itemType?.innerText.indexOf('多项选择') > 0) {
        examTypeStr = 'checkbox'
    }
        // 题干
    let itemTopic = getOneBySelect('.question > .content > .ques', '题干', false)
    let desc = itemTopic?.innerText.split('\.')[1].trim()
    let descSize = itemTopic?.innerText.split('\.')[0].trim()
    // 限制desc的长度
    desc = desc?.substring(0,50)
    // 去除换行符
    desc = desc?.replaceAll('\r','%').replaceAll('\n','%')
    let querStr = `/api/uibot/getAnswer?v=${configInfo.value.key}&examType=${examTypeStr}&desc=${desc}&examLevel=${examLevel}`
    let body = {
        "url":'/api/uibot/getAnswer',
        "v":configInfo.value.key,
        "desc":desc,
        "examLevel":examLevel,
        "descSize":descSize,
        "examType": examTypeStr
        }
    if(debugInfo.value){
        console.log(body);
    }
    
    return body
     
}

// 自动选择题 查询并点击
const choiceOptionQueryHandle = async (examLevel: string) => {
    await needStop(forceStop.value)
    let querReturn = await getChoiceQueryBody(examLevel)
    let examTypeStr = querReturn.examType
    if (allA.value){
        await needStop(forceStop.value)
        let itemOptions = getOneBySelectList('.question > .content label', '选项', false)
        itemOptions[0].click()
        await sleep(50)
        return
    }

    // 查询答案
    let resp: any = await GmPost(configInfo.value.url + querReturn.url,querReturn)
    let respObj = JSON.parse(resp)
    if(debugInfo.value){
        console.log(respObj);
    }
    if (respObj.data && respObj.data.length > 0) {
        await needStop(forceStop.value)
        // 勾选
        // console.log(respObj);
        let itemOptions = await getOneBySelectList('.question > .content label', '选项', false)
        for (const element of itemOptions) {
            await needStop(forceStop.value)
            // 选项是否被选中
            //let classname = element.className
            let text = element.innerText.split('\.')[1].trim()
            if (respObj.data.includes(text)) {
                printLog('第' + querReturn.descSize + '题: '+'选择 => ' + text)
                element.click()
                if (examTypeStr === 'radio') {
                    // 单选题跳出循环
                    break
                }
            }
            await sleep(20)
        }
    }else{
        printLog( '第' + querReturn.descSize + '题: ' + respObj.msg,'warn')

    }
    await sleep(20)


}





</script>

<style scoped></style>
